package com.iwhalecloud.bss.kite.cucc.service.map;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IUnicomRegionService;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfServiceBus;
import com.iwhalecloud.bss.kite.cucc.client.api.map.IMapAidQueryService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.AreaCode;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.AreaInquiry;
import com.iwhalecloud.bss.kite.cucc.client.dto.map.MapAidTokenReqDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.map.MapAidTokenRspDTO;
import com.iwhalecloud.bss.kite.cucc.service.util.MD5Util;
import com.iwhalecloud.bss.kite.cucc.service.util.MapAidUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Description 地图辅助查询
 * @Author deng.gang
 * @Date 2022/2/17 7:55 下午
 */
@Service
public class MapAidQueryService implements IMapAidQueryService {

    @Autowired
    private IInfServiceBus infServiceBus;

    @Autowired
    private IUnicomRegionService unicomRegionService;

    @Override
    public MapAidTokenRspDTO getToken(MapAidTokenReqDTO mapAidTokenReqDTO) {
        DcPublic keyConfig = DcPublicCache.get("2022021801", "MAP_AID_QUERY");
        if (Objects.isNull(keyConfig)) {
            ErrorConsts.IS_EMPTY.throwOut("", "", "[2022021801][MAP_AID_QUERY]配置信息为空！");
        }
        String systemName = keyConfig.getCodea(); // 调用方系统名称
        String systemAuth = keyConfig.getCodeb(); // 授权码
        String key = keyConfig.getCodec(); // AES加密密钥
        String timestamp = String.valueOf(System.currentTimeMillis()); // 当前时间戳
        String checkCode = getCheckCode(systemName, systemAuth, timestamp);

        Map<String, Object> param = new HashMap();
        param.put("systemName", MapAidUtil.encrypt(systemName, key));
        param.put("timestamp", MapAidUtil.encrypt(timestamp, key));
        param.put("checkCode", MapAidUtil.encrypt(checkCode, key));
        // 查询token
        String token = infServiceBus.getMapAidToken(param);
        MapAidTokenRspDTO mapAidTokenRspDTO = new MapAidTokenRspDTO();
        mapAidTokenRspDTO.setToken(MapAidUtil.decrypt(token, key));
        if (KiteStringUtils.isNotEmpty(mapAidTokenReqDTO.getProvinceCode())) {
            // 区域编码转换
            AreaInquiry areaInquiry = new AreaInquiry();
            areaInquiry.setGradeId(1);
            areaInquiry.setCode(mapAidTokenReqDTO.getProvinceCode());
            AreaCode areaCode = unicomRegionService.regionTransform(areaInquiry);
            if(Objects.nonNull(areaCode) && KiteStringUtils.equals(areaCode.getCode(), "00000")) {
                mapAidTokenRspDTO.setProvinceCode(areaCode.getProvinceCode());
            }
        }
        return mapAidTokenRspDTO;
    }

    private static String getCheckCode(String systemName, String systemAuth, String timestamp) {
        StringBuffer sb = new StringBuffer();
        sb.append("systemName=").append(systemName).append("&");
        sb.append("systemAuth=").append(systemAuth).append("&");
        sb.append("timestamp=").append(timestamp);
        return MD5Util.encode(sb.toString(), StandardCharsets.UTF_8.name());
    }

}
